package com.sanpu.kxe.business.trade.provider;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.text.TextUtils;

import com.sanpu.kxe.business.trade.bean.PayCollectionOutput;
import com.sanpu.kxe.business.trade.bean.RechargeDetailOutput;
import com.sanpu.kxe.business.trade.bean.TransactionOutput;
import com.sanpu.kxe.business.trade.contract.TradeConfig;
import com.sanpu.kxe.business.trade.view.ChargerTelFeeActivity;
import com.sanpu.kxe.business.trade.view.ChargerTelSetupPayActivity;
import com.sanpu.kxe.business.trade.view.ChargerTelSuccessActivity;
import com.sanpu.kxe.business.trade.view.FastPayComfirmActivity;
import com.sanpu.kxe.business.trade.view.GatheringActivity;
import com.sanpu.kxe.business.trade.view.PaymentQRcodeActivity;
import com.sanpu.kxe.business.trade.view.QuickPayWebActivity;
import com.sanpu.kxe.business.trade.view.QuickPayWebViewActivity;
import com.sanpu.kxe.business.trade.view.TransChooseDateActivity;
import com.sanpu.kxe.business.trade.view.TransactionActivity;
import com.sanpu.kxe.business.trade.view.TransactionDetailsActivity;
import com.sanpu.kxe.business.trade.view.WithdrawDetailsActivity;
import com.sanpu.kxe.business.trade.view.WithdrawListActivity;
import com.sanpu.kxe.common.enums.PayType;
import com.sanpu.kxe.jump.eventbus.FilterEvent;
import com.sanpu.kxe.utils.DateUtils;
import com.sanpu.kxe.utils.IntentTool;

import java.util.List;

/**
 * Description：交易模块内部跳转
 * Created by 薛勇军 on 2018/8/9.
 */

public class TradeOpenProvider {

    private static TradeOpenProvider instance;

    public static TradeOpenProvider getInstance() {
        if (instance == null) {
            synchronized (TradeOpenProvider.class) {
                if (instance == null) {
                    instance = new TradeOpenProvider();
                }
            }
        }
        return instance;
    }

    /**
     * 提现结果页
     */
    public void openWithdrawDetailsActivity(Context context, String withdrawMoney, boolean isOk, String remark) {
        Intent intent = new Intent(context, WithdrawDetailsActivity.class);
        intent.putExtra(TradeConfig.INTENT_AMOUNT, withdrawMoney);
        intent.putExtra(TradeConfig.INTENT_ISOK, isOk);
        intent.putExtra(TradeConfig.INTENT_REMARK, remark);
        IntentTool.startActivity(context, intent, true);
    }

    /**
     * 账单详情
     */
    public void openTransactionDetailsActivity(Context context, TransactionOutput transactionOutput) {
        Intent builder = new Intent(context, TransactionDetailsActivity.class);
        builder.putExtra(TradeConfig.INTENT_TRANSACTION, transactionOutput);
        IntentTool.startActivity(context, builder);
    }

    /**
     * 账单日期筛选
     */
    public void openTransChooseDateActivity(Context context, FilterEvent filterEvent) {
        Intent builder = new Intent(context, TransChooseDateActivity.class);
        builder.putExtra(TradeConfig.INTENT_FILTER, filterEvent);
        IntentTool.startActivity(context, builder);
    }

    /**
     * 跳转到快捷H5收款
     */
    public void openQuickPayWebActivity(Context context, String amount, PayType payType, PayCollectionOutput collectionOutput) {
        Intent intent = new Intent(context, QuickPayWebActivity.class);
        intent.putExtra(TradeConfig.INTENT_REQAMOUNT, amount);
        intent.putExtra(TradeConfig.INTENT_REQTYPE, payType);
        intent.putExtra(TradeConfig.INTENT_STLTYPE, collectionOutput.getSettleType());
        intent.putExtra(TradeConfig.INTENT_MAXFEEAMT, collectionOutput.getCapAmount());
        intent.putExtra(TradeConfig.INTENT_RLID, collectionOutput.getAppCnlConfId());
        List<PayCollectionOutput.RateDetail> rateDetails = collectionOutput.getRateDetails();
        String tradeRate = "0", otherFeeAmt = "0";
        if (rateDetails != null) {
            if (rateDetails.size() == 1) {
                tradeRate = rateDetails.get(0).getTradeRate();
                otherFeeAmt = rateDetails.get(0).getExtraCharge();
            } else {
                for (PayCollectionOutput.RateDetail rateDetail : rateDetails) {
                    if (!TextUtils.equals("VIP", rateDetail.getMerchantLevel())) {
                        tradeRate = rateDetail.getTradeRate();
                        otherFeeAmt = rateDetail.getExtraCharge();
                        break;
                    }
                }
            }
        }
        intent.putExtra(TradeConfig.INTENT_RATE, tradeRate);
        intent.putExtra(TradeConfig.INTENT_OTHERFEEAMT, otherFeeAmt);
        intent.putExtra(TradeConfig.INTENT_NEEDPIC, collectionOutput.getIsNeedPicture());
        IntentTool.startActivity(context, intent);
    }

    /**
     * 跳转到快捷H5支付
     */
    public void openQuickPayWebViewActivity(Context context, String webUrl, String postData) {
        Intent intent = new Intent(context, QuickPayWebViewActivity.class);
        intent.putExtra(TradeConfig.INTENT_WEB_URL, webUrl);
        intent.putExtra(TradeConfig.INTENT_WEB_DATA, postData);
        IntentTool.startActivity(context, intent);
    }

    /**
     * 跳转到快捷native支付
     */
    public void openFastPayComfirmActivity(Context context, String amount, PayType payType, PayCollectionOutput collectionOutput) {
        Intent intent = new Intent(context, FastPayComfirmActivity.class);
        intent.putExtra(TradeConfig.INTENT_REQAMOUNT, amount);
        intent.putExtra(TradeConfig.INTENT_REQTYPE, payType);
        List<PayCollectionOutput.RateDetail> rateDetails = collectionOutput.getRateDetails();
        String tradeRate = "0", otherFeeAmt = "0";
        if (rateDetails != null) {
            if (rateDetails.size() == 1) {
                tradeRate = rateDetails.get(0).getTradeRate();
                otherFeeAmt = rateDetails.get(0).getExtraCharge();
            } else {
                for (PayCollectionOutput.RateDetail rateDetail : rateDetails) {
                    if (!TextUtils.equals("VIP", rateDetail.getMerchantLevel())) {
                        tradeRate = rateDetail.getTradeRate();
                        otherFeeAmt = rateDetail.getExtraCharge();
                        break;
                    }
                }
            }
        }
        intent.putExtra(TradeConfig.INTENT_RATE, tradeRate);
        intent.putExtra(TradeConfig.INTENT_OTHERFEEAMT, otherFeeAmt);
        intent.putExtra(TradeConfig.INTENT_STLTYPE, collectionOutput.getSettleType());
        intent.putExtra(TradeConfig.INTENT_MAXFEEAMT, collectionOutput.getCapAmount());
        intent.putExtra(TradeConfig.INTENT_RLID, collectionOutput.getAppCnlConfId());
        intent.putExtra(TradeConfig.INTENT_NEEDPIC, collectionOutput.getIsNeedPicture());
        IntentTool.startActivity(context, intent);
    }

    /**
     * 扫码收款
     */
    public void openPaymentQRcodeActivity(Context context, String amount, PayType mPayType, String appCnlConfId)  {
        Intent builder = new Intent(context, PaymentQRcodeActivity.class);
        builder.putExtra(TradeConfig.INTENT_AMOUNT, amount);
        builder.putExtra(TradeConfig.INTENT_PAYTYPE, mPayType);
        builder.putExtra(TradeConfig.INTENT_RLID, appCnlConfId);
        IntentTool.startActivity(context, builder);
    }

    /**
     * 跳转到话费流量充值
     */
    public void openChargerTelFeeActivity(Context context) {
        Intent intent = new Intent(context, ChargerTelFeeActivity.class);
        IntentTool.startActivity(context, intent);
    }

    /**
     * 充值付款信息确认页面（金额，支付方式）
     */
    public void openChargerTelSetupPayActivity(Context context, String phoneNumber,
                                               RechargeDetailOutput.AmountPackage callcharge,
                                               RechargeDetailOutput.PointPackage integral) {
        Intent intent = new Intent(context, ChargerTelSetupPayActivity.class);
        if (callcharge != null) {
            intent.putExtra(TradeConfig.INTENT_AMOUNTPACKAGE, callcharge);
        } else if (integral != null) {
            intent.putExtra(TradeConfig.INTENT_POINTPACKAGE, integral);
        }
        intent.putExtra(TradeConfig.INTENT_PHONENUMBER, phoneNumber);
        IntentTool.startActivity(context, intent);
    }

    /**
     * 充值话费支付成功
     */
    public void openChargerTelSuccessActivity(Context context, String pkgInfo) {
        Intent intent = new Intent(context, ChargerTelSuccessActivity.class);
        intent.putExtra(TradeConfig.INTENT_PKGINFO, pkgInfo);
        IntentTool.startActivity(context, intent);
    }

    /**
     * 二维码支付被扫码支付成功展示
     */
    public void openPaySuccessActivity(Context context, boolean isUpgrade, String amount, String timestamp) {
        Uri uri = Uri.parse(String.format("%s://%s", "PaySuccessActivity", context.getPackageName())).buildUpon()
                .appendQueryParameter("amount", amount)
                .appendQueryParameter("isUpgrade", String.valueOf(isUpgrade))
                .appendQueryParameter("timestamp",
                        DateUtils.string2Date(timestamp, "yyyy-MM-dd HH:mm:ss").getTime() + "").build();
        Intent intent = new Intent(String.format("%s.business.trade.view.%s", context.getPackageName(), "PaySuccessActivity"));
        intent.setData(uri);
        IntentTool.startActivity(context, intent);
    }

    /**
     * 打开收款
     */
    public void openGatheringActivity(Context context, boolean isPersonal) {
        Intent intent = new Intent(context, GatheringActivity.class);
        if (isPersonal) {
            intent.putExtra(TradeConfig.INTENT_GATHERTYPE, TradeConfig.GATHERING_QUICKPAY);
        } else {
            intent.putExtra(TradeConfig.INTENT_GATHERTYPE, TradeConfig.GATHERING_PAY);
        }
        IntentTool.startActivity(context, intent);
    }

    /**
     * 账单
     */
    public void openTransactionActivity(Context context) {
        IntentTool.startActivity(context, TransactionActivity.class);
    }

    /**
     * 提现账单
     */
    public void openWithdrawListActivity(Context context) {
        IntentTool.startActivity(context, WithdrawListActivity.class);
    }
}
